public class AlternateIncrement {
    private int count = 1;
    private final int MAX=100;
    private final Object lock = new Object();
    private boolean thread1Turn =true;

    public void increment(boolean isThread1) throws InterruptedException {
        while (true){
            synchronized (lock){
                while (thread1Turn!=isThread1){
                    lock.wait();
                }
                if(count>MAX){
                    lock.notifyAll();
                    break;
                }
                System.out.println(Thread.currentThread().getName()+":"+count);
                count++;
                thread1Turn = !thread1Turn;
                lock.notifyAll();
            }
        }
    }
    public static void main(String[] args) {
        AlternateIncrement al = new AlternateIncrement();
        Thread thread1=new Thread(()-> {
            try {
                al.increment(true);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"Thread-1");
        Thread thread2=new Thread(()-> {
            try {
                al.increment(false);
            }catch (InterruptedException e){
                throw new RuntimeException(e);
            }
        },"Thread-2");
        thread1.start();
        thread2.start();
    }
}